#include <stdio.h>
#include "strlib.h"
#include "simpio.h"

void printGrayCode(int nBits);
void generateGrayCode(int nBits, int bit, string grayCode);
void reverseGrayCode(int nBits, int bit, string grayCode);

int main() {
	printf("ALAL\n\nEnter number of bits:\n");
	printGrayCode(GetInteger());
	return 0;
}

void printGrayCode(int nBits) {
	printf("%d bit Graycode\n\n", nBits);
	generateGrayCode(nBits, 0, "");
	reverseGrayCode(nBits, 1, "");
}

void generateGrayCode(int nBits, int bit, string grayCode) {
	grayCode = Concat(grayCode, IntegerToString(bit));
	if (nBits == 1) printf("%s\n", grayCode);
	else {
		generateGrayCode(nBits-1, 0, grayCode);
		reverseGrayCode(nBits-1, 1, grayCode);
	}
}

void reverseGrayCode(int nBits, int bit, string grayCode) {
	grayCode = Concat(grayCode, IntegerToString(bit));
	if (nBits == 1) printf("%s\n", grayCode);
	else {
		generateGrayCode(nBits-1, 1, grayCode);
		reverseGrayCode(nBits-1, 0, grayCode);
	}
}